package com.mcm.lstudy.dao.impl;

import java.util.List;

import com.mcm.lstudy.dao.CommentDao;
import com.mcm.lstudy.entity.Comment;
import com.mcm.lstudy.tools.PageData;
import com.mcm.lstudy.tools.DBTools;

public class CommentDaoImpl implements CommentDao {

	@Override
	public boolean AddComment(Comment comment) {
		if (comment != null) {
			// 调用工具类的方法
			String sql = "insert into comment values(null,?,?,?,?,?,?)";
			int n = DBTools.exUpdate(sql, comment.getVid(), comment.getUid(), comment.getCmcontent(), comment.getCmrp(),
					comment.getCmstatus(), comment.getCmdatetime());
			return n > 0;
		}
		return false;

	}

	@Override
	public boolean deleteComment(Integer cmid) {
		if (cmid != null) {
			// 调用工具类的方法
			String sql = "delete from comment where cmid=?";
			int n = DBTools.exUpdate(sql, cmid);
			return n > 0;
		}
		return false;
	}

	@Override
	public boolean updateComment(Comment comment) {
		if (comment != null) {
			// 调用工具类的修改的方法
			String sql = "update comment set cmcontent=?,cmdatetime=? WHERE cmid =?";
			int n = DBTools.exUpdate(sql, comment.getCmcontent(), comment.getCmdatetime(), comment.getCmid());
			return n > 0;
		}
		return false;
	}

	@Override
	public List<Comment> getComments(String keywords) {
		String sql = "select * from comment where cmcontent like ?";
		List<Comment> list = (List<Comment>) DBTools.exQuery(sql, Comment.class, "%" + keywords + "%");
		return list;
	}

	@Override
	public Comment getCommentById(int cmid) {
		String sql = "select * from comment where cmid=?";
		List<Comment> list = (List<Comment>) DBTools.exQuery(sql, Comment.class, cmid);
		return list.get(0);
	}

	@Override
	public Comment getCommentByCmrp(int cmrp) {
		String sql = "select * from comment where cmrp=?";
		List<Comment> list = (List<Comment>) DBTools.exQuery(sql, Comment.class, cmrp);
		return list.get(0);
	}

	@Override
	public PageData<Comment> getCommentByPage(int page, int pageSize, String keywords) {
		PageData<Comment> pd = DBTools.exQueryByPage("select * from comment where cmcontent like ?", Comment.class, page,
				pageSize, "%" + keywords + "%");
		return pd;
	}

}
